package com.manage.web.controller.function;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.util.DateUtils;
import com.manage.common.core.domain.entity.SysUser;
import com.manage.function.domain.vo.DeptInfo;
import com.manage.function.domain.vo.Expenditure;
import com.manage.function.domain.vo.Time;
import com.manage.function.domain.vo.UserAndDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.manage.common.annotation.Log;
import com.manage.common.core.controller.BaseController;
import com.manage.common.core.domain.AjaxResult;
import com.manage.common.enums.BusinessType;
import com.manage.function.domain.FunctionExpense;
import com.manage.function.service.IFunctionExpenseService;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author jinghj
 * @date 2025-03-09
 * 提供接口给前端访问
 */
@Api(value ="/function/expense",  tags = "财务支出管理接口")
@RestController
@RequestMapping("/function/expense")
public class FunctionExpenseController extends BaseController {

    @Autowired
    private IFunctionExpenseService functionExpenseService;

    /**
     * 根据实体获取数据
     */
    @ApiOperation(value = "根据实体获取数据" , notes = "根据实体获取数据" , httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('function:expense:list')")
    @PostMapping("/selectByEntity")
    public TableDataInfo selectByEntity(@ApiParam(value = "FunctionExpense查询条件", required = false) @RequestBody FunctionExpense functionExpense)
    {
        startPage();
        List<FunctionExpense> list = functionExpenseService.selectFunctionExpenseList(functionExpense);
        return getDataTable(list);
    }

    /**
     * 导出数据Excle文件
     */
    @ApiOperation(value = "导出列表", notes = "将列表数据导出为 Excel 文件",httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('function:expense:export')")
    @Log(title = "财务支出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam(value = "FunctionExpense查询条件", required = false)  FunctionExpense functionExpense)
    {
        List<FunctionExpense> list = functionExpenseService.selectFunctionExpenseList(functionExpense);
        ExcelUtil<FunctionExpense> util = new ExcelUtil<FunctionExpense>(FunctionExpense.class);
        util.exportExcel(response, list, "财务支出数据");
    }

    /**
     * 获取单条详细信息
     */
    @ApiOperation(value = "获取单条详细信息", notes = "获取单条详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('function:expense:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "id主键", required = true) @PathVariable("id") Long id)
    {
        return success(functionExpenseService.selectFunctionExpenseById(id));
    }

    /**
     * 新增数据接口
     */
    @ApiOperation(value = "新增数据" , notes = "新增数据" , httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('function:expense:add')")
    @Log(title = "财务支出", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "FunctionExpense新增数据", required = true) @RequestBody FunctionExpense functionExpense)
    {
        return toAjax(functionExpenseService.insertFunctionExpense(functionExpense));
    }

    /**
     * 更新数据接口
     */
    @ApiOperation(value = "更新数据" , notes = "更新数据" , httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('function:expense:edit')")
    @Log(title = "财务支出", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult update(@ApiParam(value = "FunctionExpense修改数据", required = true) @RequestBody FunctionExpense functionExpense)
    {
        return toAjax(functionExpenseService.updateFunctionExpense(functionExpense));
    }

    /**
     * 批量删除数据接口
     */
    @ApiOperation(value = "根据ids删除数据" , notes = "根据ids删除数据" , httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('function:expense:remove')")
    @Log(title = "财务支出", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult deleteList(@ApiParam(value = "ids主键数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(functionExpenseService.deleteFunctionExpenseByIds(ids));
    }

    //获取用户和部门信息
    @ApiOperation(value = "根据实体获取数据", notes = "根据实体获取数据", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('function:expense:list')")
    @PostMapping("/selectUserAndDept")
    public TableDataInfo selectByEntityAndDept()
    {
        startPage();
        List<DeptInfo> deptInfos = functionExpenseService.selectUserAndDept();
        return getDataTable(deptInfos);
    }
    /**
     * 获取每个部门总人数返回给前端柱状图和折线图
     * @return
     */
    @PostMapping("/getCountExpenditure")
    // @PreAuthorize("@ss.hasPermi('system:user:list')")
    @ApiOperation("获取每个部门总人数返回给前端柱状图和折线图")
    public AjaxResult getColumnar(@RequestBody Time time ){
        List<String> list=new ArrayList<>();
        List<BigDecimal> list1=new ArrayList<>();
        List<Expenditure> expenditures = functionExpenseService.countExpenditure(time.getMonth());
        if (expenditures.size()>0 && expenditures!=null){
            for (Expenditure expenditure : expenditures) {
                list.add(expenditure.getDept_name());
                list1.add(expenditure.getTotal_expense());
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("xAxis",list);
        map.put("yAxis",list1);
        return success(map);
    }
}
